<template>
  <div>
    <el-form :model="forgetPassword" label-width="100px" style="width: 600px;margin: 0 auto">
      <el-form-item>
        <p v-if="time" style="margin-left: 50%;transform: translateX(-50%)">首先请先输入电话号码以尝试寻找账户</p>
        <p v-else style="margin-left: 50%;transform: translateX(-50%)">随后请输入余下所有数据以修改密码</p>
      </el-form-item>
      <el-form-item label="电话号码">
        <el-input v-model="forgetPassword.phone" :readonly="!time"/>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="forgetPassword.username" :disabled="time"/>
      </el-form-item>
      <el-form-item label="验证码">
        <el-input v-model="forgetPassword.randomCode" :disabled="time"/>
      </el-form-item>
      <el-form-item label="新密码">
        <el-input show-password v-model="forgetPassword.newPassword" :disabled="time"/>
      </el-form-item>
      <el-form-item>
        <div style="margin-left: 50%;transform: translateX(-25%)">
          <el-button type="success" @click="submit">提交</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "forgetPassword",
  data() {
    return {
      time: true,
      forgetPassword: {
        phone: "",
        randomCode: "",
        username: "",
        newPassword: ""
      },
    }
  },
  methods: {
    submit() {
      if (this.time) {
        let url = "http://localhost:9999/zx-ums/user/remPassword?phone=" + this.forgetPassword.phone
        this.axios.get(url).then((response) => {
          let jsonResult = response.data;
          if (jsonResult.state === 200) {
            this.$message({message: "验证码已发送至手机（redis）", type: "success", duration: 6000})
            this.time = false
          } else {
            // this.$message({message: jsonResult.message, type: "error"})
            this.$message({message: "错误信息", type: "error"})
          }
        })
      } else {
        let url = "http://localhost:9999/zx-ums/user/updateRemPassword"
        let data = this.qs.stringify(this.forgetPassword);
        this.axios.post(url, data).then((response) => {
          let jsonResult = response.data;
          if (jsonResult.state === 200) {
            this.$message({message: "修改完毕，还请铭记密码", type: "success"})
            this.$router.push("/frontState")
          } else {
            this.$message({message: "验证用数据不正确，修改失败", type: "error"})
          }
        })
      }


    }
  }
}
</script>

<style scoped>

</style>